package data.posts;


import is.ruframework.data.RuData;
import models.factory.PostFactory;
import models.general.AbstractRumblrPost;
import models.general.PostDTO;
import models.general.PostDetailDTO;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;

import java.util.*;

public class PostData extends RuData implements PostDataGateway
{
  public void addPost(AbstractRumblrPost post, String username)
  {
    SimpleJdbcInsert insertPost =
        new SimpleJdbcInsert(getDataSource())
            .withTableName("ru_posts")
            .usingGeneratedKeyColumns("id");
    Map<String, Object> parameters = new HashMap<String, Object>(3);
    parameters.put("date", post.getDate());
    parameters.put("type", post.getType());
    parameters.put("username", username);
    int id = insertPost.executeAndReturnKey(parameters).intValue();
    post.setId(id);

    Collection<PostDetailDTO> details = PostFactory.createPostDetailsFromPost(post, username);
    for (PostDetailDTO pd : details)
    {
      addDetails(pd);
    }
  }

  private void addDetails(PostDetailDTO pd)
  {
    SimpleJdbcInsert insertPostDetail = new SimpleJdbcInsert(getDataSource());
    insertPostDetail.withTableName("ru_post_details");
    Map<String, Object> parameters = new HashMap<String, Object>(4);
    parameters.put("postid", pd.getPostid());
    parameters.put("name", pd.getName());
    parameters.put("value", pd.getValue());
    parameters.put("username", pd.getUsername());
    insertPostDetail.execute(parameters);
  }

    /**
     * Retrieves the posts of both the user as well as those he follows to display on dashboard
     * @param username
     * @return
     */
  public Collection<AbstractRumblrPost> getPosts(String username)
  {
    Collection<PostDTO> posts;
    Collection<AbstractRumblrPost> postReturn = new ArrayList<AbstractRumblrPost>();

    SimpleJdbcTemplate simpleJdbcTemplate = new SimpleJdbcTemplate(getDataSource());
    posts = (Collection<PostDTO>)simpleJdbcTemplate.query("select distinct ru_posts.* from ru_posts where" +
                                                           " ru_posts.username =? order by date desc",
                                                            new PostRowMapper(), username);
    posts.addAll((Collection<PostDTO>)simpleJdbcTemplate.query("select distinct ru_posts.* from ru_posts, ru_follows where" +
                                                              " ru_follows.username = ? order by date desc",
                                                                new PostRowMapper(), username));

    AbstractRumblrPost newPost = null;
    ArrayList<PostDetailDTO> postDetails;
    for(PostDTO post : posts)
    {
      postDetails = (ArrayList<PostDetailDTO>)simpleJdbcTemplate.query("select * from ru_post_details where postid=?",
        new PostDetailRowMapper(), post.getId());

        newPost = (AbstractRumblrPost) PostFactory.createPostFromDTO(post, postDetails);

      postReturn.add(newPost);
    }


    return postReturn;
  }

    /**
     * Adds a like to the ru_post_details table, taking in postId to know which post is being liked
     * and username to know who liked the post.
     * @param postId
     * @param username
     */
    public void addLike(int postId, String username)
    {
       String sqlUsername = "select distinct username from ru_post_details where postid = ?";

       JdbcTemplate getJdbcTemplate = new JdbcTemplate(getDataSource());
       String postUsername = (String)getJdbcTemplate.queryForObject(sqlUsername, new Object[]{postId}, String.class);

       SimpleJdbcInsert insertPostDetail = new SimpleJdbcInsert(getDataSource());
       insertPostDetail.withTableName("ru_post_details");
       Map<String, Object> parameters = new HashMap<String, Object>(4);

       parameters.put("postid", postId);
       parameters.put("name", "like");
       parameters.put("value", username);
       parameters.put("username", postUsername);
       insertPostDetail.execute(parameters);

    }

    /**
     * Retrieves all likes for a certain post. Takes in the post's id.
     * @param postId
     * @return
     */
    public int getLikes(int postId)
    {
        SimpleJdbcTemplate simpleJdbcTemplate = new SimpleJdbcTemplate(getDataSource());

        List<PostDetailDTO> likeList = (List<PostDetailDTO>)simpleJdbcTemplate.query("select * from ru_post_details " +
                                                                                    "where name = 'like' and postid=?",
                                                                                     new PostDetailRowMapper(), postId);
        return likeList.size();
    }
}
